package service.impl;

import dao.IAssetsHmbmDao;
import dao.IHisDataDao;
import dao.IPacsDataDao;
import dao.IPerformanceBaseDataDao;
import model.AssetsHmbm;
import model.HisData;
import model.PacsData;
import model.PerformanceBaseData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.PerformanceBaseDadaServiceOne;
import util.UUIDGenerator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author: 杨青桦
 * @Description: 绩效基本数据获取并插入数据库service实现类
 * @Date: 2017/7/6 17:15
 */
@Service
public class PerformanceBaseDataServiceOneImpl implements PerformanceBaseDadaServiceOne {

    private static Log logger = LogFactory.getLog(PerformanceBaseDataServiceOneImpl.class);

    //设备信息
    @Autowired
    private IAssetsHmbmDao iAssetsHmbmDao;

    //his数据
    @Autowired
    private IHisDataDao iHisDataDao;

    @Autowired
    private IPacsDataDao iPacsDataDao;

    @Autowired
    private IPerformanceBaseDataDao iPerformanceBaseDataDao;


    /**
     * @Author: 杨青桦
     * @Description:  控江pacs绩效分析
     * @Date: 2017/10/13 10:47
     * @param hospitalId
     * @param pageSize
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setPacsData(String hospitalId, Integer pageSize) {
        Map<String,Object> paramMap = new HashMap<String, Object>();
        paramMap.put("pageStart",0);
        paramMap.put("pageSize",pageSize);
        paramMap.put("hospitalId",hospitalId);
        List<HisData> list = iHisDataDao.queryPacsHisData(paramMap);
        for(HisData hisData : list) {
            PacsData pacsData = null;
            //有申请号的情况且能和pacs记录匹配上
            if(hisData.getRequestnum() != null && !"".equals(hisData.getRequestnum())){
                pacsData = iPacsDataDao.selectByRequestNum(hisData.getRequestnum());
            }else{
                String mznum = hisData.getMzNum();
                String zynum = hisData.getZyNum();
                //无申请单，使用门诊号和住院号匹配
                if(mznum != null && !"".equals(mznum)){
                    pacsData = iPacsDataDao.selectByMzOrZyNum(mznum,null);
                } else if(zynum != null && !"".equals(zynum)){
                    pacsData = iPacsDataDao.selectByMzOrZyNum(null,zynum);
                } else {
                    pacsData = null;
                }
            }
            //his记录能和pacs记录匹配到
            if(pacsData != null){
                AssetsHmbm assetsHmbm = iAssetsHmbmDao.queryByPacsName(pacsData.getEqpname());
                PerformanceBaseData performanceBaseData = new PerformanceBaseData();
                performanceBaseData.setId(UUIDGenerator.getUniqueLong());
                performanceBaseData.setAssetsId(assetsHmbm.getId());
                performanceBaseData.setTime(hisData.getSfrq());
                performanceBaseData.setHmbm(hisData.getHmbm());
                performanceBaseData.setSfname(hisData.getSfname());
                performanceBaseData.setHospitalId(assetsHmbm.getHospitalId());
                performanceBaseData.setAmount(hisData.getMoney1());
                performanceBaseData.setType(0);
                performanceBaseData.setAssetsName(assetsHmbm.getEqpname());
                performanceBaseData.setUsedCounts(1.0);
                iPerformanceBaseDataDao.insert(performanceBaseData);
            }else{//pacs中无记录
                //所有pacs设备列表
                List<AssetsHmbm> pacsAssetsList = iAssetsHmbmDao.queryPacsAssetsList();
                for(AssetsHmbm assetsHmbm : pacsAssetsList){
                    int size = pacsAssetsList.size();
                    //查询无关联pacs设备收费时，设备是否有维修
                    Map<String,Object> map = new HashMap<String, Object>();
                    map.put("sfrq",hisData.getSfrq());
                    map.put("assetsId",assetsHmbm.getId());
                    Integer isRepairing = iAssetsHmbmDao.assetsIsRepairing(map);
                    if(isRepairing != null && isRepairing > 0){
                        size --;
                        continue;
                        //按照金额比值分配收入
                    }else {
                        PerformanceBaseData performanceBaseData = new PerformanceBaseData();
                        performanceBaseData.setId(UUIDGenerator.getUniqueLong());
                        performanceBaseData.setAssetsId(assetsHmbm.getId());
                        performanceBaseData.setTime(hisData.getSfrq());
                        performanceBaseData.setHmbm(hisData.getHmbm());
                        performanceBaseData.setSfname(hisData.getSfname());
                        performanceBaseData.setHospitalId(assetsHmbm.getHospitalId());
                        performanceBaseData.setAmount(hisData.getMoney1()/(size*1.0));
                        performanceBaseData.setType(0);
                        performanceBaseData.setUsedCounts(1.0/(size*1.0));
                        performanceBaseData.setAssetsName(assetsHmbm.getEqpname());
                        iPerformanceBaseDataDao.insert(performanceBaseData);
                    }
                }
            }
            //分析好的数据改变分析状态
            iHisDataDao.updateHisData(hisData.getId());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateIsAnalyze(Integer tag) {
        return iPerformanceBaseDataDao.updateIsAnalyze(tag);
    }

    /**
     * @Author: 杨青桦
     * @Description:  控江非pacs设备绩效分析
     * @Date: 2017/10/13 10:48
     * @param hospitalId
     * @param pageSize
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setDataExcludePacs(String hospitalId, Integer pageSize) {
        Map<String,Object> paramMap = new HashMap<String, Object>();
        paramMap.put("pageStart",0);
        paramMap.put("pageSize",pageSize);
        paramMap.put("hospitalId",hospitalId);
        List<HisData> list = iHisDataDao.queryHisDataExcludePacs(paramMap);
        for(HisData hisData : list){
            List<AssetsHmbm> assetsList = iAssetsHmbmDao.queryByHmbm(hisData.getHmbm());
            //黄码编码找不到，不分析
            if(assetsList.size() == 0){
                continue;
                //黄码编码只对应一个设备，his的收入即为这个设备的收入
            }else if(assetsList.size() == 1){
                PerformanceBaseData performanceBaseData = new PerformanceBaseData();
                performanceBaseData.setId(UUIDGenerator.getUniqueLong());
                performanceBaseData.setAssetsId(assetsList.get(0).getId());
                performanceBaseData.setTime(hisData.getSfrq());
                performanceBaseData.setHmbm(hisData.getHmbm());
                performanceBaseData.setSfname(hisData.getSfname());
                performanceBaseData.setHospitalId(assetsList.get(0).getHospitalId());
                performanceBaseData.setAmount(hisData.getMoney1());
                performanceBaseData.setType(0);
                performanceBaseData.setAssetsName(assetsList.get(0).getEqpname());
                performanceBaseData.setUsedCounts(1.0);
                iPerformanceBaseDataDao.insert(performanceBaseData);
                //对应多个设备
            }else{
                //计算除了当时报修设备的总金额
                Long totalPrice = getTotalPrice(assetsList,hisData);
                for(AssetsHmbm assetsHmbm : assetsList){
                    int size = assetsList.size();
                    Map<String,Object> map = new HashMap<String, Object>();
                    map.put("sfrq",hisData.getSfrq());
                    map.put("assetsId",assetsHmbm.getId());
                    Integer isRepairing = iAssetsHmbmDao.assetsIsRepairing(map);
                    if(isRepairing != null && isRepairing > 0){
                        size --;
                        continue;
                        //按照金额比值分配收入
                    }else{
                        PerformanceBaseData performanceBaseData = new PerformanceBaseData();
                        performanceBaseData.setId(UUIDGenerator.getUniqueLong());
                        performanceBaseData.setAssetsId(assetsHmbm.getId());
                        performanceBaseData.setTime(hisData.getSfrq());
                        performanceBaseData.setHmbm(hisData.getHmbm());
                        performanceBaseData.setSfname(hisData.getSfname());
                        performanceBaseData.setHospitalId(assetsHmbm.getHospitalId());
                        performanceBaseData.setAmount(hisData.getMoney1()*assetsHmbm.getPrice()/totalPrice);
                        performanceBaseData.setType(0);
                        performanceBaseData.setAssetsName(assetsHmbm.getEqpname());
                        performanceBaseData.setUsedCounts(1.0/(size*1.0));
                        iPerformanceBaseDataDao.insert(performanceBaseData);
                    }
                }
            }
            iHisDataDao.updateHisData(hisData.getId());
        }
    }


    /**
     * @Author: 杨青桦
     * @Description: 获取该条his记录涉及的所有设备金额总和(正在维修中的除外)
     * @Date: 2017/7/7 12:13
     * @param assetsList
     * @param hisData
     * @return
     */
    private Long getTotalPrice(List<AssetsHmbm> assetsList,HisData hisData){
        Long totalPrice = 0L;
        for(AssetsHmbm assetsHmbm : assetsList){
            Map<String,Object> paramMap = new HashMap<String, Object>();
            paramMap.put("sfrq",hisData.getSfrq());
            paramMap.put("assetsId",assetsHmbm.getId());
            Integer isRepairing = iAssetsHmbmDao.assetsIsRepairing(paramMap);
            if(isRepairing != null && isRepairing > 0){
                continue;
            }else{
                if(assetsHmbm.getPrice() == null){
                    assetsHmbm.setPrice(0L);
                }
                totalPrice += assetsHmbm.getPrice();
            }
        }
        return totalPrice;
    }




}
